﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Serialization;
using Sharp.Portal.Interface;
using Sharp.Portal.Mvc.List;
using Sharp.Portal.Other;

namespace Sharp.Portal.AdminLTE.Provider
{
    /// <summary>
    /// 树形列表页适配器
    /// </summary>
    public class ListTreePage : ITemplateProvider
    {
        private static readonly CamelCasePropertyNamesContractResolver Cpncr = new CamelCasePropertyNamesContractResolver();
        /// <summary>
        /// 获取模板
        /// </summary>
        /// <param name="model"></param>
        /// <param name="isMobileDevice"></param>
        /// <returns></returns>
        public string GetTemplate(object model, bool isMobileDevice)
        {
            var dataList = (BSSharpTreeList)model;
            //dataList.IsMobileDevice = isMobileDevice;
            dataList.InitComplete();

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("@model {0}\r\n", dataList.GetType().FullName);

            sb.AppendLine("@if (ViewBag.IsPjaxRequest == false)");
            sb.AppendLine("{");
            sb.AppendLine("Layout = \"~/Areas/Platform/Views/Shared/__Layout.cshtml\";");
            sb.AppendLine("}");
            sb.AppendLine("<section class=\"content-header\">");

            sb.AppendLine("<h1> @Model.PageTitle </h1>");

            if (dataList.Breadcrumb.IsNotNullOrEmpty())
            {
                sb.AppendLine(dataList.Breadcrumb);
            }
            sb.AppendLine("</section>");
            sb.AppendLine("<section class=\"content\">");
            sb.AppendLine("<div class=\"box box-primary container\">");
            sb.AppendLine("<div class=\"box-header with-border\">");

            if (!dataList.Feature.HasFlag(ListFeature.HiddenToolButton))
            {
                sb.AppendLine("<div class=\"row\">");
                sb.AppendLine("<div class=\"dataTable-tools btn-group col-md-6 col-sm-12 no-padding\">");

                //渲染工具栏按钮
                var toolButtons = string.Join("\r\n", dataList.ToolButtons.Select(x => x.Value.Html));
                sb.AppendLine(toolButtons);
                sb.AppendLine("</div>");
            }


            sb.AppendLine("<div style=\"display:none;\" class=\"box-tools SearchArea col-md-6 col-sm-12\">");
            sb.AppendLine("<div class=\"input-group input-group-sm\">");

            foreach (var column in dataList.QueryItems)//渲染默认查询项
            {
                if (column.QueryFeatureOption.HasFlag(QueryFeature.DefualtQuery))
                {
                    sb.AppendFormat("<input type =\"text\" name=\"{0}\" autocomplete=\"off\" class=\"form-control pull-right\" placeholder=\"{1}\">\r\n", column.Name, column.Text);
                    break;
                }
            }
            sb.AppendLine("<div class=\"input-group-btn\">");
            sb.AppendLine("<button type =\"button\" class=\"btn btn-default\" onclick=\"window.app.search(this);\"><i class=\"fa fa-search\"></i></button>");

            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("<div class=\"box-body no-padding\">");


            sb.AppendLine("<div class=\"table-responsive\">");
            sb.AppendFormat("<table id=\"{0}\" class=\"table table-bordered table-striped table-condensed table-hover\">", dataList.ListControlId);
            sb.AppendLine("<thead><tr>");

            if (dataList.Feature.HasFlag(ListFeature.MultiSelect))
            {
                sb.AppendLine("<th style=\"width:43px;\" class=\"center\"><label class=\"i-checks\"><input type=\"checkbox\" /><i></i></label></th>");
            }
            foreach (var column in dataList.ColumnItems)
            {
                if (column.Hidden) continue;

                var jsonName = Cpncr.GetResolvedPropertyName(column.DataIndex);

                sb.AppendFormat("@if (this.CheckFieldPermission(\"{0}\")) {{\r\n", jsonName);
                if (column.Feature.HasFlag(ColumnFeature.NumberColumn))
                {
                    sb.AppendFormat("<th {1} class=\"text-right\">{0}</th>\r\n", column.Text,
                        column.Width > 0 ? "style=\"width:" + column.Width + "px;\"" : "");
                }
                else
                {
                    sb.AppendFormat("<th {1}>{0}</th>\r\n", column.Text,
                        column.Width > 0 ? "style=\"width:" + column.Width + "px;\"" : "");
                }
                sb.AppendLine("}");
            }
            sb.AppendLine("                                </tr></thead>");
            sb.AppendLine("                            <tbody>");
            sb.AppendLine("                                @{");
            sb.AppendLine("                                    foreach (var item in Model.Data)");
            sb.AppendLine("                                    {");
            sb.AppendLine("                                        <tr class=\"@item.Itemclass\">");
            if (dataList.Feature.HasFlag(ListFeature.MultiSelect))
            {
                sb.AppendLine("<td style=\"width:43px;\" class=\"center checkColumn\"><label class=\"i-checks\"><input type=\"checkbox\" name=\"cellCheckbox\" value=\"@item.Id\" /><i></i></label></td>");
            }
            foreach (var column in dataList.ColumnItems)
            {
                if (column.Hidden) continue;

                var jsonName = Cpncr.GetResolvedPropertyName(column.DataIndex);

                sb.AppendFormat("@if (this.CheckFieldPermission(\"{0}\")) {{\r\n", jsonName);

                if (!column.MasterDataCategory.IsNullOrEmpty())
                {
                    sb.AppendLine($"<td>@Model.GetMasterDataText(\"{column.MasterDataCategory}\", item.{column.Name})</td>");
                }
                else if (column.Feature.HasFlag(ColumnFeature.DateColumn))
                {
                    sb.AppendFormat("<td>@item.{0}.ToString(\"{1}\")</td>\r\n", column.Name, column.Format);
                }
                else if (column.Feature.HasFlag(ColumnFeature.TemplateColumn) || column.Feature.HasFlag(ColumnFeature.TreeColumn))
                {
                    if (column.Template.IsNullOrEmpty())
                        sb.AppendFormat("<td>@item.{0}</td>\r\n", column.Name);
                    else
                        sb.AppendFormat("<td>{0}</td>\r\n", column.Template);
                }
                else if (column.Feature.HasFlag(ColumnFeature.NumberColumn))
                {
                    sb.AppendFormat("<td class=\"text-right\">@item.{0}</td>\r\n", column.Name);
                }
                else
                {
                    sb.AppendFormat("<td>@item.{0}</td>\r\n", column.Name);
                }

                sb.AppendLine("}");
            }

            sb.AppendLine("</tr>");
            sb.AppendLine("}");
            sb.AppendLine("}");
            sb.AppendLine("</tbody></table>");

            if (dataList.ImportJavascript.IsNotNullOrEmpty())
            {
                if (dataList.ImportJavascript.StartsWith("~") || dataList.ImportJavascript.StartsWith(@"\"))
                {
                    sb.AppendFormat("<script src=\"{0}\"></script>\r\n", dataList.ImportJavascript);
                }
            }
            if (dataList.ImportCss.IsNotNullOrEmpty())
            {
                if (dataList.ImportCss.StartsWith("~") || dataList.ImportCss.StartsWith(@"\"))
                {
                    sb.AppendFormat("<link href=\"{0}\" rel=\"stylesheet\" />\r\n", dataList.ImportCss);
                }
            }

            var treeColumn = 0;
            if (dataList.Feature.HasFlag(ListFeature.MultiSelect)) treeColumn++;
            if (dataList.Feature.HasFlag(ListFeature.RowNumber)) treeColumn++;
            foreach (var column in dataList.ColumnItems)
            {
                if (column.Hidden) continue;
                if (column.Feature.HasFlag(ColumnFeature.TreeColumn))
                {
                    break;
                }
                treeColumn++;
            }
            sb.AppendFormat("<script>$('#{0}').treegrid({{ treeColumn: {1} }});</script>\r\n", dataList.ListControlId, treeColumn);

            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("</section>");

            return sb.ToString();
        }
    }
}
